package com.uphone.magicleafteacher.view;

import android.Manifest;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.base.BaseGActivity;
import com.uphone.magicleafteacher.base.OnPermissionCallBack;
import com.uphone.magicleafteacher.util.RecordUtils;
import com.zlw.main.recorderlib.recorder.listener.RecordResultListener;

public class RecordLoadingView extends View {

    private final Context mContext;
    private int defaultPic = R.drawable.ic_recording;
    private Bitmap defaultBitmap;
    private Paint mPaint;
    private Paint mBgPaint;
    private Paint mCenterPaint;
    private Paint mRingPaint;
    private Paint mProgressPaint;
    private int mProgress;
    private RectF mRectFOut;
    private RectF mRectFBg;
    private RectF mRectFCenter;
    private boolean isFinish = true;

    public RecordLoadingView(Context context) {
        this(context, null);
    }

    public RecordLoadingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RecordLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;

        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(defaultBitmap.getWidth(), defaultBitmap.getHeight());
    }

    private int ringWidth = 4;

    private void init() {
        mPaint = new Paint();
        defaultBitmap = BitmapFactory.decodeResource(getResources(), defaultPic);

        mBgPaint = new Paint();
        mBgPaint.setColor(getResources().getColor(R.color.themecolor));
        mBgPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mBgPaint.setAntiAlias(true);

        mCenterPaint = new Paint();
        mCenterPaint.setColor(getResources().getColor(R.color.white));
        mCenterPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mCenterPaint.setAntiAlias(true);

        mRingPaint = new Paint();
        mRingPaint.setColor(getResources().getColor(R.color.gray9));
        mRingPaint.setStyle(Paint.Style.STROKE);
        mRingPaint.setStrokeWidth(ringWidth);
        mRingPaint.setAntiAlias(true);

        mProgressPaint = new Paint();
        mProgressPaint.setColor(getResources().getColor(R.color.themecolor));
        mProgressPaint.setStyle(Paint.Style.STROKE);
        mProgressPaint.setStrokeWidth(ringWidth);
        mProgressPaint.setAntiAlias(true);

        mRectFOut = new RectF(ringWidth, ringWidth, defaultBitmap.getWidth() - ringWidth, defaultBitmap.getHeight() - ringWidth);
        outSpace += ringWidth;
        int bgX = defaultBitmap.getWidth() - outSpace * 2;
        int bgY = defaultBitmap.getHeight() - outSpace * 2;
        mRectFBg = new RectF(outSpace, outSpace, bgX + outSpace, bgY + outSpace);
        int centerWidth = bgX / 3;
        int centerHeight = bgY / 3;
        mRectFCenter = new RectF(centerWidth + outSpace, centerHeight + outSpace, centerWidth * 2 + outSpace, centerHeight * 2 + outSpace);

    }

    private boolean isPlaying = false;
    private int outSpace = 6;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!isPlaying) {
            mHandler.removeMessages(0);
            canvas.drawBitmap(defaultBitmap, 0, 0, mPaint);
        } else {
            canvas.drawOval(mRectFOut, mRingPaint);
            canvas.drawOval(mRectFBg, mBgPaint);
            canvas.drawRoundRect(mRectFCenter, 5, 5, mCenterPaint);
            canvas.drawArc(mRectFOut, 0f, mProgress * 360f / maxProgress, false, mProgressPaint);
            if (isFinish) {
                mProgress = maxProgress;
                mHandler.sendEmptyMessageDelayed(0, 80);
            } else {
                mProgress = (mProgress + 1) % maxProgress;
                mHandler.removeMessages(0);
            }
        }
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (isPlaying) {
                invalidate();
            }
        }
    };

    private int maxProgress = 100;


    public boolean isPlaying() {
        return isPlaying;
    }

    public boolean isFinish() {
        return isFinish;
    }

    public void setPlaying(BaseGActivity activity, boolean playing) {
        if (!playing) {
            isPlaying = playing;
            invalidate();
            return;
        }
        activity.getPermission(
                new OnPermissionCallBack() {
                    @Override
                    public void permissionPass(String[] permissions) {
                        isPlaying = playing;
                        mProgress = 0;
                        isFinish = true;
                        invalidate();
                        if (isPlaying) {
                            // 录音
                            RecordUtils.init().doPlay();
                        }
                    }

                    @Override
                    public void permissionRefuse(String[] permissions) {
                        ToastUtils.showShort("您已禁止权限，请到设置中心打开权限");
                    }
                }, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.RECORD_AUDIO);
    }

    public void setFinish(RecordResultListener listener) {
        mProgress = maxProgress;
        isFinish = false;
        invalidate();
        // 结束
        RecordUtils.init().doStop(listener);
    }
}
